//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2006
//
// Routing graph remover.
//

# include "StdAfx.h"
# include "Resource.h"
# include "GenerateRGNodes.h"
# include "UndoRedo.h"
# include "Types.h"
# include "GarminTypes.h"
# include "Status.h"
# include "MapView.h"
# include "fast_floor.h"
# include "Globals.h"

void _RemoveRGNodes () {
	CCritSec::CLock lock (g_cs);

	rgns_t & polylines = g_map.layers [rkPolyline].rgns;

	const size_t cTotalRgns = g_map.layers [rkPolyline].cRgns;
	if (cTotalRgns == 0)
		return;
	size_t cRgnsProcessed = 0;

	SetStatus (GetStr (IDS_STATUS_REMOVING));
	SetProgressStatus (0);

	for (rgns_t::iterator iRgn = polylines.begin (); iRgn != polylines.end (); ++ iRgn) {
		rgn_t & polyline = * iRgn;
		if (polyline.IsDeleted () || polyline.IsWTR ())
			continue;

		const size_t cElements = polyline.elements.size ();
		for (size_t c = 0; c < cElements; ++ c) {
			rgn_element_t & element = polyline.elements [c];
			if (element.pExtra && ! element.pExtra->PointExtras.empty ()) {
				element.pExtra->PointExtras.clear ();
				polyline.dwFlags |= rgn_t::fModified;
			}
		}

		++ cRgnsProcessed;
		SetProgressStatus (cRgnsProcessed*100/cTotalRgns);
	}

	g_map.CountRGNodes ();

	SetStatusReady ();
}

////////////////////////////////////////////////////////////

class CRemoveRGNodesAction : public CEditAction {
  public:
	virtual void GetName (string_t & _str) const {_str.append ("remove routing nodes");}
	virtual cu_t CanUndo () const {return cuBackup;}
	virtual bool Do ();
	virtual void Undo () {}
	virtual void Redo () {}
};

bool CRemoveRGNodesAction::Do () {
	_RemoveRGNodes ();
	Refresh ();
	return true;
}

///////////////////////////////

void RemoveRGNodes () {
	PerformEditAction (new CRemoveRGNodesAction ());
}